/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package web;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import persistence.entity.Account;
import session.stateless.AccountFacadeRemote;

/**
 *
 * @author Craig
 */
@FacesConverter(forClass=AccountConverter.class, value="accountConverter")
public class AccountConverter implements Converter {
    AccountFacadeRemote accountFacade = lookupAccountFacadeRemote();

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        Account acc = accountFacade.find(Integer.parseInt(value));
        return acc; 
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        return "" + ((Account) value).getAccountId();
    }

    private AccountFacadeRemote lookupAccountFacadeRemote() {
        try {
            Context c = new InitialContext();
            return (AccountFacadeRemote) c.lookup("java:global/Entity/Entity-ejb/AccountFacade!session.stateless.AccountFacadeRemote");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
